using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using MediaPortal.Configuration;

namespace MyEmulators2
{
    public enum TranslatorString
    {
        viewlist,
        viewlargeicons,
        viewicons,
        viewfilmstrip,
        mostplayed,
        recentlyplayed,
        topgraded,
        moreviews,
        sortgenre,
        sortcompany,
        sortyeardesc,
        sortyearasc,
        selectgrade,
        setgradeto,
        nograde,
        favourites,
        favouritemark,
        favouriteunmark,
        pcgames,
        never,
        allgames,
        viewcoverflow,
        launchwithprofile
    }

    public class Translator
    {
        private static Dictionary<TranslatorString, String> dictionary;

        public static void init()
        {
            dictionary = new Dictionary<TranslatorString, String>();

            dictionary.Add(TranslatorString.viewlist, "Layout: List");
            dictionary.Add(TranslatorString.viewlargeicons, "Layout: Large Icons");
            dictionary.Add(TranslatorString.viewicons, "Layout: Icons");
            dictionary.Add(TranslatorString.viewfilmstrip, "Layout: Filmstrip");
            dictionary.Add(TranslatorString.mostplayed, "Most played");
            dictionary.Add(TranslatorString.recentlyplayed, "Recently played");
            dictionary.Add(TranslatorString.topgraded, "Top graded");
            dictionary.Add(TranslatorString.moreviews, "More views");
            dictionary.Add(TranslatorString.sortgenre, "Sort by genre");
            dictionary.Add(TranslatorString.sortcompany, "Sort by company");
            dictionary.Add(TranslatorString.sortyeardesc, "Sort by year (newest first)");
            dictionary.Add(TranslatorString.sortyearasc, "Sort by year (oldest first)");
            dictionary.Add(TranslatorString.selectgrade, "Select grade for this game");
            dictionary.Add(TranslatorString.setgradeto, "Set grade to");
            dictionary.Add(TranslatorString.nograde, "No grade");
            dictionary.Add(TranslatorString.favourites, "Favourites");
            dictionary.Add(TranslatorString.favouritemark, "Mark as favourite");
            dictionary.Add(TranslatorString.favouriteunmark, "Unmark as favourite");
            dictionary.Add(TranslatorString.pcgames, "PC Games");
            dictionary.Add(TranslatorString.never, "Never");
            dictionary.Add(TranslatorString.allgames, "All Games");
            dictionary.Add(TranslatorString.viewcoverflow, "Layout: Coverflow");
            dictionary.Add(TranslatorString.launchwithprofile, "Launch with profile...");

            String transFolder = Config.GetFolder(Config.Dir.Language) + @"\myEmulators";
            if (!Directory.Exists(transFolder))
            {
                Directory.CreateDirectory(transFolder);
            }

            //Load the translation file
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(transFolder + "\\" + Options.Instance.GetStringOption("language") + ".xml");
                XmlNodeList nodes = doc.GetElementsByTagName("translatedstring");
                foreach (XmlNode node in nodes)
                {
                    String key = node.Attributes.GetNamedItem("key").Value;
                    String value = node.InnerText;
                    object finder = Enum.Parse(typeof(TranslatorString), key);
                    dictionary[(TranslatorString)finder] = value;
                }
            }
            catch (Exception)
            {
                //Could not open the selected translation file, 
                //so use the standard english sentences
            }
        }

        public static String[] getLanguages()
        {
            String transFolder = Config.GetFolder(Config.Dir.Language) + @"\myEmulators";
            List<String> languages = new List<String>();
            foreach (String languageFile in Directory.GetFiles(transFolder, "*.xml"))
            {
                String temp = languageFile.Substring(languageFile.LastIndexOf("\\") + 1);
                languages.Add(temp.Remove(temp.Length - 4));
            }
            return languages.ToArray();
        }

        public static String getString(TranslatorString str)
        {
            return dictionary[str];
        }
    }
}
